/**
 * Created by huangxuehua on 2018/1/18.
 */
//$("header>div.goBack").click(function(){
//        window.history.go(-1);
//});
var xpHome= new Vue({
    el:"#xpApp",
    data: {
        addressList: [],//地址列表
        isShowEditInfo:false,//编辑地址
        selectAddress:{},//当前选择的地址
        isCheck:true,//是否默认地址
    },
    methods:{
       initData:function(){
          var addressInfo= window.localStorage.getItem("editAddressInfo");
           if(addressInfo){
               xpHome.addressList=JSON.parse(addressInfo);
               //console.log("-- xpHome.addressList---", xpHome.addressList);
           }
               xpHome.selectAddress =  xpHome.addressList;
               //console.log("--  xpHome.selectAddress----",  xpHome.selectAddress)
               xpHome.isCheck = xpHome.selectAddress.is_def == 1?true:false;
               //console.log("==xpHome.selectAddress==",xpHome.selectAddress);
               //vue 加载慢 显示问题
               setTimeout(function(){
                   var area1 = new LArea();
                   area1.init({
                       'trigger': '#demo1', //触发选择控件的文本框，同时选择完毕后name属性输出到该位置
                       'valueTo': '#value1', //选择完毕后id属性输出到该位置
                       'keys': {
                           id: 'value',
                           name: 'text'
                       }, //绑定数据源相关字段 id对应valueTo的value属性输出 name对应trigger的value属性输出
                       'type': 1, //数据源类型
                       'data': LAreaData //数据源
                   });

                   var cityName = xpHome.selectAddress.province_name+
                       xpHome.selectAddress.city_name+
                       xpHome.selectAddress.district_name;
                   $("#demo1").val(cityName );//设置选中值

                   //暂时有问题
                   area1.value=[xpHome.selectAddress.province,//省份
                       xpHome.selectAddress.city,//城市
                       xpHome.selectAddress.district];//控制初始位置，注意：该方法并不会影响到input的value
                   $("#value1").val(xpHome.selectAddress.province+","+//省份
                       xpHome.selectAddress.city+","+//城市
                       xpHome.selectAddress.district);
               },300);
       },


        checkBtn:function() {//默认地址按钮
            xpHome.isCheck = !xpHome.isCheck;
        },
        submitEditAddress:function() {   //保存编辑地址
            var city = $("#value1").val();
            var citys = city.split(",");
            var data = {
                //"userid":userInfo.userid,
                "data[address_id]	": xpHome.selectAddress.address_id,
                "data[is_def]": xpHome.isCheck == true ? 1 : 0,
                "data[tel]": xpHome.selectAddress.tel,
                "data[email]": xpHome.selectAddress.email,
                "data[consignee]": xpHome.selectAddress.consignee,
                "data[country]": "1",
                "data[province]": citys[0],
                "data[city]": citys[1],
                "data[district]": citys[2],
                "data[address]": xpHome.selectAddress.address,
            }
            //console.log("///////data//////",data)
            $.ajax({
                url: nUrls.setEditDefaultAddress,
                type: 'POST',
                data: data,
                success: function (data) {
                    //console.log("+++++++++++++"+data)
                    var result = eval('(' + data + ')');
                    if (result && result.code == 200) {

                        var toast = new auiToast(); //初始化toast事件
                        toast.custom({
                            title: "修改地址成功",
                            html: '',
                            duration: 2000
                        })
                        //var flag = nUtils.getUrlParam("flag");
                        //if(flag == "edit") {
                        //    window.location.href="./my_address.html?flag=edit";
                        //} else if(flag="mycenter_edit"){
                        //    window.location.href="./my_address.html?flag=mycenter_edit";
                        //}
                        window.history.back();
                    }
                },
                error: function () {
                    console.log("获取信息出错");
                }
            });
        }
    }
})

$(function(){
    xpHome.initData();
})